angular.module('Controller')

.controller 'WFWelcomeCtrl', ($scope,wfAlert,$q,userData,menuManage,authorityManage, $rootScope, $http) ->
    $scope.userInfo=userData.getUser()
    $scope.info={
        apartmentId:userData.getStore()
        showStoreInfo:false
    }
    $rootScope.$on('menu-loaded',(obj,menus)->
        $scope.info.showStoreInfo=_.some(menus,(item)->item.href=='welcome')
    )
    $scope.lettingRateData={
        labels:[]
        datasets:[
            {
                label:'门店出租率(%)'
                data : []
                "fill": false,
                "backgroundColor": [
                    "rgba(255, 99, 132, 0.2)","rgba(255, 159, 64, 0.2)",
                    "rgba(255, 205, 86, 0.2)","rgba(75, 192, 192, 0.2)",
                    "rgba(54, 162, 235, 0.2)", "rgba(153, 102, 255, 0.2)",
                    "rgba(201, 203, 207, 0.2)"],
                "borderColor": [
                    "rgb(255, 99, 132)", "rgb(255, 159, 64)",
                    "rgb(255, 205, 86)", "rgb(75, 192, 192)",
                    "rgb(54, 162, 235)", "rgb(153, 102, 255)",
                    "rgb(201, 203, 207)"],
                "borderWidth": 1
            }
        ]
        list:[]
    }
    $scope.apartmentRentList=[]
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'所有公寓'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
                $scope.apartmentRentList=resp.message.content
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.headerPartList=[
        {key:'emptyHouse',name:'空置房间',value:'- -',type:'current',typeName:'当前'}
        {key:'rentOutHouse',name:'已出租房间',value:'- -',type:'current',typeName:'当前'}
        {key:'gathering',name:'今日收款(元)',value:'- -',type:'today',typeName:'今日'}
    ]
    $scope.currentInfoList=[
        {key:'newContract',name:'新签合同',value:'- -',type:'today',typeName:'今日'}
        {key:'lettingRate',name:'出租率',value:'- -',type:'current',typeName:'当前'}
        {key:'countAppointment',name:'预约看房',value:'- -',type:'today',typeName:'今日'}
        {key:'totalMoneyMonth',name:'当月收入',value:'- -',type:'month',typeName:'本月'}
        {key:'expireContract',name:'即将到期合同',value:'- -',type:'today',typeName:'今日',link:'contract-manage'}
        {key:'expireBill',name:'即将到期账单',value:'- -',type:'today',typeName:'今日',link:'rent-bill-manage'}
        {key:'customerComplaint',name:'用户投诉',value:'- -',type:'current',typeName:'当前'}
        {key:'totalMoneyYear',name:'今年收入',value:'- -',type:'year',typeName:'今年'}
    ]
    lineChartOptions={
        bezierCurve:false
    }
    loadStatic=()->
        reqData={apartmentId:$scope.info.apartmentId}
        $http.get('/statistic',params:reqData)
        .success((resp)->
            console.log(resp.message)
            if resp.code==0
                $scope.data=res=resp.message
                _.each($scope.currentInfoList,(item)->
                    if res[item.key]!=undefined
                        item.value=res[item.key]
                )
                _.each($scope.headerPartList,(item)->
                    if res[item.key]!=undefined
                        item.value=res[item.key]
                )
                $scope.appointDataSet=transferChartData(res['visiHouseList'],'providerTime','count','近七日内看房人数','date')
                $scope.contractDataSet=transferChartData(res['houseRentList'],'signTime','count','30日内新增合同','date5')
                if res['lettingRateList'] and res['lettingRateList'].length>0
                    _.each res['lettingRateList'],(item)->
                        tmp=_.find $scope.apartmentRentList,(theAp)->theAp.id==item.apartmentId
                        if tmp
                            tmp.rent=item.rent
                            tmp.rate=(item.rate*100).toFixed(2)
                    $scope.lettingRateData.labels=[]
                    $scope.lettingRateData.datasets[0].data=[]
                    _.each $scope.apartmentRentList,(item)->
                        $scope.lettingRateData.labels.push(item.name)
                        $scope.lettingRateData.datasets[0].data.push(item.rate || 0)
                loadChart()
            else
                ''
        )
    loadChart=()->
        #折线图
        if $scope.appointDataSet
            _.defer(()->
                ctx = $("#appointment-count-chart").get(0).getContext("2d")
                config1={
                    type:'line',
                    data:$scope.appointDataSet
                    options:{
                        legend:{
                            display: true,
                            labels:{
                                fontSize: 16
                            }
                        }
                        scales:{
                            yAxes:[{ticks:{stepSize:1}}]
                        }
                    }
                }
                myNewChart=new Chart(ctx,config1)
            )
        if $scope.contractDataSet
            _.defer(()->
                ctx2 = $("#contract-increase-chart").get(0).getContext("2d")
                config2={
                    type:'line',
                    data:$scope.contractDataSet
                    options:{
                        legend:{
                            display: true,
                            labels:{
                                fontSize: 16
                            }
                        }
                        scales:{
                            yAxes:[{ticks:{stepSize:1}}]
                        }
                    }
                }
                myNewChart2=new Chart(ctx2,config2)
            )
        if 1==1
            _.defer(()->
                ctx3 = $("#apartment-rent-chart").get(0).getContext("2d")
                config3={
                    type:'bar',
                    data:$scope.lettingRateData
                    options:{
                        legend:{
                            display: true,
                            labels:{
                                fontSize: 16
                            }
                        }
                        scales: {
                            yAxes: [{"ticks": {
                                "beginAtZero": true,
                                "max":100
                            }}]
                        }
                    }
                }
                myNewChart3=new Chart(ctx3,config3)
            )
        #柱状图

    $scope.pickApartment=()->
        loadStatic()
    _.extend $scope,
        clickItem:(item)->
            if item.link
                menuManage.goState(item.link,true)
    transferChartData=(resList,label,val,title,labFormat)->
        return undefined if resList==undefined || resList.length==0 || resList[0][label]==undefined
        labels=[]
        valList=[]
        _.each resList,(item,index)->
            if item[label]!=undefined and item[val]!=undefined
                theLb=item[label]
                if labFormat=='date' and theLb.length==10
                    theLb=theLb.substring(5)
                if labFormat=='date5' and theLb.length==10
                    theLb=''#theLb.substring(8)
#                    if index%5==0
#                        theLb=theLb.substring(8)
#                    else
#                        theLb=''
                labels.push(theLb)
                valList.push(item[val])
        data = {
            labels : labels,
            datasets : [
                {
                    label:title
                    lineTension:0.1
                    backgroundColor: "rgba(75,192,192,0)",
                    borderColor: "rgba(75,192,192,1)",
                    data : valList
                }
            ]
        }
        data
    loadStatic()



